﻿using ShopFine.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ShopFine
{
    public partial class SubCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //LoadCategories();
                string subID = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(subID))
                {
                    LoadProductBySubCateID(subID);
                }
            }
        }

        //private void LoadCategories()
        //{
        //    var categories = new CategoryDAO().GetAll();
        //    rptCategories.DataSource = categories;
        //    rptCategories.DataBind();
        //}

        private void LoadProductBySubCateID(string subID)
        {
            var subCategory = new SubCategoryDAO().GetSubCategoryByID(Convert.ToInt32(subID));
            titleSubCategory.Text = subCategory == null ? "N/A" : string.Format("<a href='CategoryList.aspx?id={2}'>{0}</a> -> {1}", subCategory.Category.Name, subCategory.Name, subCategory.CategoryID);
            var products = new ProductDAO().GetProductBySubCateID(Convert.ToInt32(subID));
            rptProducts.DataSource = products;
            rptProducts.DataBind();
        }

        public string IsSelected(string cateID)
        {
            string urlNamePage = Request.Url.Segments[Request.Url.Segments.Length - 1].ToLower();
            string subID = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(subID))
            {
                bool isSelected = new SubCategoryDAO().CheckCateSelected(Convert.ToInt32(cateID), Convert.ToInt32(subID));
                return isSelected ? "invarseColor active" : "invarseColor";
            }
            return "invarseColor";
        }

        public string IsSubSelected(string subCateID)
        {
            string urlNamePage = Request.Url.Segments[Request.Url.Segments.Length - 1].ToLower();
            string subCurrentID = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(subCurrentID))
            {
                bool isSelected = new SubCategoryDAO().CheckSubCateSelected(Convert.ToInt32(subCateID), Convert.ToInt32(subCurrentID));
                return isSelected ? "invarseColor active" : "invarseColor";
            }
            return "invarseColor";
        }

        protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Category cate = e.Item.DataItem as Category;
                Repeater rptSubCates = e.Item.FindControl("rptSubCates") as Repeater;
                if (rptSubCates != null && cate.SubCategories.Count > 0)
                {
                    rptSubCates.DataSource = cate.SubCategories.ToList();
                    rptSubCates.DataBind();
                }
            }
        }
    }
}